<template>
  <div class="login-container">
    <el-form ref="registerForm" :model="registerForm" :rules="registerRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">注册</h3>
      </div>

      <!-- <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="registerForm.username"
          placeholder="Username"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="registerForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item> -->
       <el-form-item label="账号" prop="userAccount" label-width="80px">
          <el-input v-model="registerForm.userAccount" placeholder="请填写账号" />
        </el-form-item>
        <el-form-item label="密码" prop="userPassword" label-width="80px">
          <el-input v-model="registerForm.userPassword" placeholder="请填写密码" />
        </el-form-item>

       <el-form-item label="用户编号" prop="userCode" label-width="80px">
          <el-input v-model="registerForm.userCode" placeholder="请填写用户编号" />
        </el-form-item>
        <el-form-item label="用户名称" prop="userName" label-width="80px">
          <el-input v-model="registerForm.userName" placeholder="请填写用户名称" />
        </el-form-item>

        <el-form-item label="性别" prop="userGender" label-width="80px">
          <el-select v-model="registerForm.userGender" class="filter-item" placeholder="请选择用户性别" >
            <el-option v-for="item in gender" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        
        <el-form-item label="用户角色" prop="userRole" label-width="80px">
          <!-- <el-input v-model="registerForm.userRole" placeholder="请选择用户角色" /> -->
          <el-select v-model="registerForm.userRole" class="filter-item" placeholder="请选择用户角色" >
            <el-option v-for="item in roles" :key="item" :label="item" :value="item" />
          </el-select>
        </el-form-item>
        <el-form-item label="电话号码" prop="userTele" label-width="80px">
          <el-input v-model="registerForm.userTele" placeholder="请填写电话号码" />
        </el-form-item>
        <el-form-item label="邮箱" prop="userEmail" label-width="80px">
          <el-input v-model="registerForm.userEmail" placeholder="请填写邮箱" />
        </el-form-item>
        <el-form-item label="状态" prop="userState" label-width="80px">
          <el-input v-model="registerForm.userState"  :disabled="true"/>
          <!-- <el-select v-model="registerForm.userState" class="filter-item" placeholder="请选择用户状态" >
            <el-option v-for="item in stateOptions" :key="item" :label="item" :value="item" />
          </el-select> -->
        </el-form-item>

      <el-button :loading="loading" type="primary" 
                 style="width:100%;margin-bottom:30px;"
                 @click="commitForm()">
                 确认</el-button>

      <el-button :loading="loading" type="primary" 
                 style="width:100%;
                        margin-bottom:30px;
                        margin-left: 0px;" 
                        @click="cancel()">
                 取消</el-button>
    </el-form>
  </div>
</template>

<script>

//自定义登录表单验证
import { validUsername } from '@/utils/validate'
import { getRegisterList } from '@/api/userRole'
import { register } from '@/api/userInfo'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码至少不少于6位字符'))
      } else {
        callback()
      }
    }
    return {
      stateOptions: ['启用', '禁用'],   //用户状态
      gender:['男','女'],     //用户性别
      roles:[],       //角色
      registerForm: {
        userAccount: '',
        userPassword: '',
        userCode:'',
        userName:'',
        userGender:'',
        userRole:'',
        userTele:'',
        userEmail:'',
        userState:'启用'
      },
      //登录表单验证
      registerRules: {
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        //验证用户编号——只能数字+英文,不能包含特殊字符
        userCode: [{ required: true, message: '用户编号不能为空', trigger: 'blur' },
          {pattern:/^[a-zA-Z0-9]+$/, message: '不能包含特殊字符', trigger: ['blur','change'] },
          {min:2, max:6 ,message:'用户编号不少于2位不长于6位',trigger:['blur','change']}],
        userName: [{ required: true, message: '用户名称不能为空', trigger:  'blur' },
          {min:2, max:10 ,message:'用户名称不少于2位不长于10位',trigger:['blur','change']}],
        userGender: [{ required: true, message: '请填写用户性别', trigger: 'blur' }],
        userRole: [{ required: true, message: '请填写用户角色', trigger: 'blur' }],
        //验证手机号码
        userTele: [{pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, required: true, message: '请填写正确的手机号码', trigger: 'change' }],
        //验证邮箱
        userEmail: [{type:'email', required: true, message: '请填写正确的邮箱', trigger: ['blur','change'] }],
        userState: [{ required: true, message: '请选择用户状态', trigger: 'change' }],
        userAccount:[{required: true, message: '用户账号不能为空', trigger: 'blur' },
          { pattern:/^[a-zA-Z0-9]+$/, required: true, message:'不能包含特殊字符',trigger:'change'},
          {min:6, max:15 ,message:'账号长度不少于6位不长于15位',trigger:['blur','change']}],
        userPassword:[{ required: true, message:'密码不能为空',trigger:'blur'},
          { pattern:/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$)([^\u4e00-\u9fa5\s]){6,20}$/,
            message:'6-20位英文字母、数字或者符号（除空格），且字母、数字和标点符号至少包含两种',
            trigger:['blur','change']},]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getRole()
  },
  methods: {
    getRole(){
      getRegisterList().then(response=>{
        this.roles = response.data.items
      })
    },
    // 确认按钮
    commitForm(){
      console.log("确认按钮被点击了")

      console.log("当前表格信息为",this.registerForm)
      this.$refs['registerForm'].validate((valid)=>{
        if(valid){
          register(this.registerForm).then(res =>{
            this.$message({
             type: 'success',
             message: '注册成功!'
            });          
          })
          this.$router.push('/login')
        }
      })
    },
    // 取消按钮
    cancel(){
      console.log("取消按钮被点击了")
      this.$message({
        type: 'info',
        message: '取消注册'
      });
      this.$router.push('/login')
    },
    //显示密码事件
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
